package com.imooc.Annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class ParseAnnotation {
    public static void main(String[] args) {
        //1 使用类加载器加载类
        try {
//            Class<?> c = Class.forName("com.imooc.Annotation.Child");
            // 这两个都可以
            Class<Child> c = Child.class;
            // 2 找到类上面的注解
            boolean isExist = c.isAnnotationPresent(Description.class);
            if(isExist){
                // 3.拿到注解实例
                Description d = c.getAnnotation(Description.class);
                System.out.println(d.desc());
            }
            // 4.找到方法上的注释
            Method[] methods = c.getDeclaredMethods();
            for (Method method : methods) {
                boolean isMExist = method.isAnnotationPresent(Description.class);
                if(isMExist){
                    Description d = method.getAnnotation(Description.class);
                    System.out.println(d.desc() + ", " + d.author() + ", " + d.age());
                }
            }

            // 4.另一种注解方法
            for (Method method : methods) {
                Annotation[] annotations = method.getAnnotations();
                for (Annotation annotation : annotations) {
                    if(annotation instanceof  Description){
                        Description d = (Description)annotation;
                        System.out.println(d.desc() + ", " + d.author() + ", " + d.age());
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
